﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HardwareAs
{
    public partial class ManageDownload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                DataSBind();

        }
        private void DataSBind()
        {
            HardwareLinqDataContext nb = new HardwareLinqDataContext();
            ListView1.DataSource = nb.SelectAllDownload();

            ListView1.DataBind();


        }

        protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
        {

            ListView1.InsertItemPosition = InsertItemPosition.None;
            (sender as ListView).EditIndex = e.NewEditIndex;
            DataSBind();

        }
        /* When Click Insert button item inserting event will response 
         * then run the storeprocedure InsertDownload to insert a record.
        * */
        protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
          
            HardwareLinqDataContext nb = new HardwareLinqDataContext();

            string Title = (ListView1.InsertItem.FindControl("txtTitle") as TextBox).Text;
            string CID = (ListView1.InsertItem.FindControl("DropDownList1") as DropDownList).SelectedValue;
            string desc = (ListView1.InsertItem.FindControl("txtDescription") as TextBox).Text;
            string downloadurl = (ListView1.InsertItem.FindControl("txtDownloadUrl") as TextBox).Text;


            HardwareLinqDataContext db = new HardwareLinqDataContext();
            db.InsertDownload(Title, desc, downloadurl, Convert.ToInt32(CID), "super", true);
            ListView1.InsertItemPosition = InsertItemPosition.None;
          DataSBind();




        }

        protected void ListView1_ItemInserted(object sender, ListViewInsertedEventArgs e)
        {
        
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ListView1.EditIndex > -1) ListView1.EditIndex = -1;
            ListView1.InsertItemPosition = InsertItemPosition.FirstItem;
            DataSBind();
        }

        protected void DropDownList2_Load(object sender, EventArgs e)
        {

            (sender as DropDownList).DataTextField = "CategoryName";
            (sender as DropDownList).DataValueField = "Id";
            HardwareLinqDataContext nb = new HardwareLinqDataContext();
            (sender as DropDownList).DataSource = nb.SelectAllCategory();

            (sender as DropDownList).DataBind();
        }

        protected void Label5_Load(object sender, EventArgs e)
        {

        }

        protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            ListView1.EditIndex = -1;
            DataSBind();
        }

        protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
           int editid=Convert.ToInt32(((Label)ListView1.EditItem.FindControl("txtId")).Text);
           string title= ((TextBox)ListView1.EditItem.FindControl("txtTitle")).Text;
           string desc = ((TextBox)ListView1.EditItem.FindControl("txtDescription")).Text;
           int Cid =Convert.ToInt32(((DropDownList)ListView1.EditItem.FindControl("DropDownList1")).SelectedValue);
           string downloadurl=((TextBox)ListView1.EditItem.FindControl("txtDownloadUrl")).Text;
           HardwareLinqDataContext db = new HardwareLinqDataContext();
           db.UpdateDownload(editid, title, desc, downloadurl, Cid);
           ListView1.EditIndex = -1;
           DataSBind();

        }

        protected void ListView1_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
        {
           
        }

        protected void DeleteDownload(object sender, EventArgs e)
        {
            HardwareLinqDataContext db = new HardwareLinqDataContext();
            db.DeleteDownload(Convert.ToInt32(((sender as Button).CommandArgument)));
            DataSBind();
        }
    }
}